package Interfaces;
import clases.Profesor;


import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class frmProfesor extends javax.swing.JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JLabel jlblCedP;
	private JButton jbtnInclP;
	private JTextField jtxtTitP;
	private JTextField jtxtApeP;
	private JTextField jtxtNomP;
	private JTextField jtxtCedP;
	private JLabel jlblTitP;
	private JLabel jlblApeE;
	private JLabel jlblNomP;


	public frmProfesor() {
                setSize(450,300);
                setLocation(300,300);
				AnchorLayout thisLayout = new AnchorLayout();
				getContentPane().setLayout(thisLayout);
				this.setTitle("Profesor");
				
				
					jbtnInclP = new JButton();
					getContentPane().add(jbtnInclP, new AnchorConstraint(746, 419, 840, 230, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jbtnInclP.setPreferredSize(new java.awt.Dimension(100, 25));
					jbtnInclP.setText("Incluir");
			
					jtxtTitP = new JTextField();
					getContentPane().add(jtxtTitP, new AnchorConstraint(531, 572, 610, 261, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jtxtTitP.setPreferredSize(new java.awt.Dimension(122, 21));
				
					jtxtApeP = new JTextField();
					getContentPane().add(jtxtApeP, new AnchorConstraint(385, 572, 464, 261, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jtxtApeP.setPreferredSize(new java.awt.Dimension(122, 21));
				
					jtxtNomP = new JTextField();
					getContentPane().add(jtxtNomP, new AnchorConstraint(234, 572, 313, 261, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jtxtNomP.setPreferredSize(new java.awt.Dimension(122, 21));
				
					jtxtCedP = new JTextField();
					getContentPane().add(jtxtCedP, new AnchorConstraint(92, 567, 171, 261, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jtxtCedP.setPreferredSize(new java.awt.Dimension(120, 21));
				
					jlblTitP = new JLabel();
					getContentPane().add(jlblTitP, new AnchorConstraint(543, 202, 595, 57, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jlblTitP.setText("Titulo :");
					jlblTitP.setPreferredSize(new java.awt.Dimension(57, 14));
				
					jlblApeE = new JLabel();
					getContentPane().add(jlblApeE, new AnchorConstraint(396, 215, 449, 57, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jlblApeE.setText("Apellido :");
					jlblApeE.setPreferredSize(new java.awt.Dimension(62, 14));
				
					jlblNomP = new JLabel();
					getContentPane().add(jlblNomP, new AnchorConstraint(246, 215, 298, 57, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jlblNomP.setText("Nombre :");
					jlblNomP.setPreferredSize(new java.awt.Dimension(62, 14));
				
					jlblCedP = new JLabel();
					getContentPane().add(jlblCedP, new AnchorConstraint(103, 215, 156, 57, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jlblCedP.setText("Cedula :");
					jlblCedP.setPreferredSize(new java.awt.Dimension(62, 14));
				
					jbtnInclP.addActionListener(this);
				
				
				}

				public void actionPerformed(ActionEvent accion) {
					if(accion.getSource()==this.jbtnInclP){
						if(frmPrincipal.seccion.getProfesor()!=null){
						  boolean resp,cedValida,validarLetras;
						  resp=validarCampos();
						  if(resp){
							cedValida=validarCedula(this.jtxtCedP.getText());
							  if(cedValida){
							  validarLetras=validarLetras(this.jtxtNomP.getText()+this.jtxtApeP.getText()+this.jtxtTitP.getText());
							  if(validarLetras){
							  Profesor profesor=new Profesor();
							  profesor.setCedula(this.jtxtCedP.getText());
							  profesor.setNombre(this.jtxtNomP.getText());
							  profesor.setApellido(this.jtxtApeP.getText());
							  profesor.setTitulo(this.jtxtTitP.getText());
							  frmPrincipal.seccion.AsignarProfesor(profesor);	
							  JOptionPane.showMessageDialog(null,"El profesor ha sido asignado exitosamente","Asignacion del profesor", 
										JOptionPane.INFORMATION_MESSAGE);
							  this.limpiar();
							  this.setVisible(false);
							  }
							  else
								  JOptionPane.showMessageDialog(null,"Debes introducir solo letras","Inscripcion del estudiante", 
											JOptionPane.ERROR_MESSAGE);
							}
							else
								 JOptionPane.showMessageDialog(null,"Introduzca ocho digitos","Asignacion del profesor", 
											JOptionPane.INFORMATION_MESSAGE);
						}
						 else
					    	  JOptionPane.showMessageDialog(null,"Debe llenar todos los datos","Asignacion del profesor", 
										JOptionPane.INFORMATION_MESSAGE);
					    }
						else
					    	  JOptionPane.showMessageDialog(null,"El profesor ya ha sido asignado","Asignacion del profesor", 
										JOptionPane.INFORMATION_MESSAGE);
					}
				}

				private boolean validarCampos()
				{
					boolean valida=false;
					if ((!this.jtxtCedP.getText().equals("")) && (!this.jtxtNomP.getText().equals("")) && (!this.jtxtApeP.getText().equals(""))&& (!this.jtxtTitP.getText().equals("")))
					    valida=true;
					
					return valida;
				}
				private void limpiar(){
					this.jtxtCedP.setText("");
					this.jtxtNomP.setText("");
					this.jtxtApeP.setText("");
					this.jtxtTitP.setText("");
					this.jtxtCedP.requestFocus(true);
					
					
				}
				private boolean validarCedula(String Ced){
					boolean valida=false;
					int resp;
					String numero="0123456789";
					char caracter;
					if(Ced.length()==8){
				    for(int i=0;i<Ced.length();i++){
				    	caracter=Ced.charAt(i);
				        resp=numero.indexOf(caracter);
				    	if(resp!=-1)
				    	valida=true;
				      }
					}
					return valida;
						
				}
				private boolean validarLetras(String letras){
					boolean valida=true;
					int resp;
					String numero="0123456789";
					char caracter;
					if(letras.length()<60){
				    for(int i=0;i<letras.length();i++){
				    	caracter=letras.charAt(i);
				        resp=numero.indexOf(caracter);
				    	if(resp!=-1)
				    	valida=false;
				    	
				      }
						
				}
			
					return valida;	
				}
				

}
